<?php

namespace App\Http\Controllers\Admin;

use App\Models\Fangattr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangAttrController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //实例化
        $model = new Fangattr();
        $data = $model->getList();
        return view('admin.fangattr.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取顶部属性
        $data = Fangattr::where('pid',0)->get();
        return view('admin.fangattr.create',compact('data'));
    }


    public function upfile(Request $request)
    {
        //默认图标
        $pic = config('up.pic');
        if($request->hasFile('file')){
            //参数2 配置的节点名称
            $ret = $request->file('file')->store('','fangattr');
            $pic = '/uploads/fangattr/'.$ret;
        }
        return ['status'=>0,'url'=>$pic];
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证
        $this->validate($request,[
            'name'=>'required'
        ]);
        $postData = $request->except(['_token','file']);

        $postData['field_name'] = !empty($postData['field_name']) ? $postData['field_name'] :'';
        Fangattr::create($postData);

        return redirect(route('admin.fangattr.index'));

    }

    //生成房源信息索引
    public function esinit()
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function show(Fangattr $fangattr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function edit(Fangattr $fangattr)
    {
        //
        $data = Fangattr::where('pid',0)->get();
        return view('admin.fangattr.edit',compact('data','fangattr'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Fangattr $fangattr)
    {
        //表单验证
        $this->validate($request,[
            'name'=>'required'
        ]);
        $postData = $request->except(['_token','file','_method']);

        $postData['field_name'] = !empty($postData['field_name']) ? $postData['field_name'] :'';
        $fangattr->update($postData);
        return redirect(route('admin.fangattr.index'));

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fangattr $fangattr)
    {
        //
        $fangattr->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
}
